/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFLogin.java
 *
 * Created on 21/10/2011, 15:09:01
 */
package view.usuario;

import control.Sessao;
import view.tecnico.JFPrincipalTecnico;
import view.admin.JFPrincipalAdmin;
import java.awt.event.KeyEvent;
import java.util.List;
import javax.swing.JOptionPane;
import model.Pessoa;
import model.persistence.PessoaDAO;
import util.Versao;

/**
 *
 * @author Felipe
 */
public class JFLogin extends javax.swing.JFrame {

    private PessoaDAO dao;
    private List<Pessoa> pessoas;
    
    
    /** Creates new form JFLogin */
    public JFLogin() {
        initComponents();
        dao = new PessoaDAO();
        pessoas = dao.getList();
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlPrincipal = new javax.swing.JPanel();
        pnlLogin = new javax.swing.JPanel();
        lblLogin = new javax.swing.JLabel();
        lblSenha = new javax.swing.JLabel();
        ptxtSenha = new javax.swing.JPasswordField();
        txtLogin = new javax.swing.JTextField();
        btnEntrar = new javax.swing.JButton();
        lblTitulo = new javax.swing.JLabel();
        lblSubtitulo = new javax.swing.JLabel();
        pnlRodape = new javax.swing.JPanel();
        btnSair = new javax.swing.JButton();
        lblEmail = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("SIGEPI - Login");
        setBackground(new java.awt.Color(102, 153, 255));
        setResizable(false);
        setUndecorated(true);

        pnlPrincipal.setBackground(new java.awt.Color(44, 44, 153));
        pnlPrincipal.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        pnlLogin.setBackground(new java.awt.Color(48, 141, 187));
        pnlLogin.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        lblLogin.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblLogin.setForeground(new java.awt.Color(255, 255, 255));
        lblLogin.setText("Usuário");

        lblSenha.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblSenha.setForeground(new java.awt.Color(255, 255, 255));
        lblSenha.setText("Senha");

        ptxtSenha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                ptxtSenhaKeyPressed(evt);
            }
        });

        txtLogin.setMaximumSize(new java.awt.Dimension(15, 20));
        txtLogin.setMinimumSize(new java.awt.Dimension(15, 20));
        txtLogin.setPreferredSize(new java.awt.Dimension(15, 20));
        txtLogin.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtLoginKeyPressed(evt);
            }
        });

        btnEntrar.setText("Entrar");
        btnEntrar.setFocusable(false);
        btnEntrar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEntrar.setOpaque(false);
        btnEntrar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnEntrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEntrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlLoginLayout = new javax.swing.GroupLayout(pnlLogin);
        pnlLogin.setLayout(pnlLoginLayout);
        pnlLoginLayout.setHorizontalGroup(
            pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlLoginLayout.createSequentialGroup()
                .addGroup(pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnlLoginLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnEntrar, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlLoginLayout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(pnlLoginLayout.createSequentialGroup()
                                .addComponent(lblLogin)
                                .addGap(21, 21, 21))
                            .addGroup(pnlLoginLayout.createSequentialGroup()
                                .addComponent(lblSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                        .addGroup(pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ptxtSenha, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                            .addComponent(txtLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE))))
                .addGap(42, 42, 42))
        );
        pnlLoginLayout.setVerticalGroup(
            pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlLoginLayout.createSequentialGroup()
                .addGroup(pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(pnlLoginLayout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(lblLogin))
                    .addGroup(pnlLoginLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(txtLogin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(8, 8, 8)
                .addGroup(pnlLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblSenha)
                    .addComponent(ptxtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnEntrar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lblTitulo.setFont(new java.awt.Font("Arial", 1, 24)); // NOI18N
        lblTitulo.setForeground(new java.awt.Color(255, 255, 255));
        lblTitulo.setText(Versao.NOME);

        lblSubtitulo.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        lblSubtitulo.setForeground(new java.awt.Color(255, 255, 255));
        lblSubtitulo.setText("Sistema Gestor de Parque de Informática");

        javax.swing.GroupLayout pnlPrincipalLayout = new javax.swing.GroupLayout(pnlPrincipal);
        pnlPrincipal.setLayout(pnlPrincipalLayout);
        pnlPrincipalLayout.setHorizontalGroup(
            pnlPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPrincipalLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(pnlPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblTitulo)
                    .addComponent(lblSubtitulo)
                    .addComponent(pnlLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(33, Short.MAX_VALUE))
        );
        pnlPrincipalLayout.setVerticalGroup(
            pnlPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPrincipalLayout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(lblTitulo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblSubtitulo)
                .addGap(18, 18, 18)
                .addComponent(pnlLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(24, Short.MAX_VALUE))
        );

        getContentPane().add(pnlPrincipal, java.awt.BorderLayout.CENTER);

        pnlRodape.setBackground(new java.awt.Color(0, 0, 0));
        pnlRodape.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        pnlRodape.setMaximumSize(new java.awt.Dimension(397, 50));
        pnlRodape.setMinimumSize(new java.awt.Dimension(397, 50));
        pnlRodape.setPreferredSize(new java.awt.Dimension(397, 50));

        btnSair.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnSair.setForeground(new java.awt.Color(255, 255, 255));
        btnSair.setText("Sair");
        btnSair.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        btnSair.setContentAreaFilled(false);
        btnSair.setDoubleBuffered(true);
        btnSair.setMinimumSize(new java.awt.Dimension(50, 19));
        btnSair.setPreferredSize(new java.awt.Dimension(50, 19));
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });
        btnSair.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnSairKeyPressed(evt);
            }
        });

        lblEmail.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblEmail.setForeground(new java.awt.Color(255, 255, 255));
        lblEmail.setText("Email: sigepi@sigepi.com.br");

        javax.swing.GroupLayout pnlRodapeLayout = new javax.swing.GroupLayout(pnlRodape);
        pnlRodape.setLayout(pnlRodapeLayout);
        pnlRodapeLayout.setHorizontalGroup(
            pnlRodapeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlRodapeLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 146, Short.MAX_VALUE)
                .addComponent(lblEmail)
                .addGap(24, 24, 24))
        );
        pnlRodapeLayout.setVerticalGroup(
            pnlRodapeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlRodapeLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlRodapeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSair, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
                    .addComponent(lblEmail))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(pnlRodape, java.awt.BorderLayout.PAGE_END);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-401)/2, (screenSize.height-287)/2, 401, 287);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        System.exit(0);
    }//GEN-LAST:event_btnSairActionPerformed

    private void btnEntrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEntrarActionPerformed
        logar();            
    }//GEN-LAST:event_btnEntrarActionPerformed

    private void ptxtSenhaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_ptxtSenhaKeyPressed
        switch (evt.getKeyCode()) {
            case KeyEvent.VK_ENTER  : logar();
                                      break;
            case KeyEvent.VK_ESCAPE : System.exit(0);
                                      break;
        }
    }//GEN-LAST:event_ptxtSenhaKeyPressed

    private void txtLoginKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtLoginKeyPressed
        switch (evt.getKeyCode()) {
            case KeyEvent.VK_ENTER  : ptxtSenha.requestFocus();
                                      break;
            case KeyEvent.VK_ESCAPE : System.exit(0);
                                      break;        
        }
    }//GEN-LAST:event_txtLoginKeyPressed

    private void btnSairKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnSairKeyPressed
        switch (evt.getKeyCode()) {
            case KeyEvent.VK_ENTER  : System.exit(0);
                                      break;
            case KeyEvent.VK_ESCAPE : System.exit(0);
                                      break;        
        }
    }//GEN-LAST:event_btnSairKeyPressed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEntrar;
    private javax.swing.JButton btnSair;
    private javax.swing.JLabel lblEmail;
    private javax.swing.JLabel lblLogin;
    private javax.swing.JLabel lblSenha;
    private javax.swing.JLabel lblSubtitulo;
    private javax.swing.JLabel lblTitulo;
    private javax.swing.JPanel pnlLogin;
    private javax.swing.JPanel pnlPrincipal;
    private javax.swing.JPanel pnlRodape;
    private javax.swing.JPasswordField ptxtSenha;
    private javax.swing.JTextField txtLogin;
    // End of variables declaration//GEN-END:variables
    
     private void logar() {
        String senha = ptxtSenha.getText();
        boolean loginValido = false;
        String inLogin = txtLogin.getText();
        String inSenha = ptxtSenha.getText();
        Pessoa usuarioLogado = null;
        
        for (int k = 0; k < pessoas.size(); k++) {
            if (inLogin.equals(pessoas.get(k).getLogin())) {
                if (ptxtSenha.getText().equals(pessoas.get(k).getSenha())) {
                    usuarioLogado = pessoas.get(k);
                    loginValido = true;
                    break;
                }
            }
        }     
        
        if (loginValido == false) {
            JOptionPane.showMessageDialog(null, "Usuário ou senha inválido!\n" +
                                                "Verifique se digitou seus dados corretamente ou contate o Administrador do Sistema");
            limparCampos();
        }
        else {
            if (usuarioLogado.isAtivo()) {
                loginPorTipo(usuarioLogado);
            } else {
                JOptionPane.showMessageDialog(null, "Este usuario foi desativado!\n" +
                                                    "Caso o erro persista contate o Administrador do Sistema");
                limparCampos();
            }     
        }
    }
    
    public void limparCampos() {
        txtLogin.setText("");
        ptxtSenha.setText("");
        txtLogin.requestFocus();
    }
    
    public void loginPorTipo(Pessoa usuarioLogado) {
        int op = 0;        
        Sessao.iniciarSessao(usuarioLogado);
        
        if (usuarioLogado.getTipo().equals("ADMINISTRADOR")) {
            JFPrincipalAdmin jfAdmin = new JFPrincipalAdmin();
            jfAdmin.setVisible(true);
            this.dispose(); 
        } else {
            if (usuarioLogado.getTipo().equals("TECNICO")){
                JFPrincipalTecnico jfTecnico = new JFPrincipalTecnico();
                jfTecnico.setVisible(true);
                this.dispose();
            } else {
                JFPrincipalUsuario jfUsuario = new JFPrincipalUsuario();
                jfUsuario.setVisible(true);
                this.dispose();                
            }
        }
    }      
}
